package com.jovision.sovplay;

import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.BaseActivity;
import com.jovision.JVNetConst;
import com.jovision.JniUtil;

/**
 * Created by juyang on 16/12/12.
 */

public class JVCatFilePlayActivity extends BaseActivity {

    private static final String TAG = "JVCatFilePlayActivity";
    private int window;
    private String playPath;

    private int currentProgress;
    private int totalProgress;
    private boolean isConnected = false;

    SurfaceHolder playSurfaceHolder;
    SurfaceView playSurfaceView;
    @Override
    protected void initSettings() {
        currentProgress = 0;
        totalProgress = 0;

        window = getIntent().getIntExtra("window",0);
        playPath = getIntent().getStringExtra("playPath");
    }

    @Override
    protected void initUi() {
        setContentView(R.layout.activity_fileplay);
        playSurfaceView = (SurfaceView) findViewById(R.id.playsurface);
        playSurfaceHolder = playSurfaceView.getHolder();
        playSurfaceHolder.addCallback(new SurfaceHolder.Callback(){
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Toast.makeText(JVCatFilePlayActivity.this, "路径:"+playPath, Toast.LENGTH_SHORT).show();
                SovUtil.enableRemotePlay(window,true);
                JniUtil.resumeSurface(window, playSurfaceHolder.getSurface());
                handler.sendEmptyMessageDelayed(111,500);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
    }

    @Override
    protected void saveSettings() {

    }

    @Override
    protected void onResume() {
        if (isConnected) {
            JniUtil.resumeSurface(window, playSurfaceHolder.getSurface());
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (isConnected) {
            JniUtil.pauseSurface(window);
        }

        super.onPause();
    }


    @Override
    protected void freeMe() {
        SovUtil.stopStreamRemotePlay(window);
        SovUtil.enableRemotePlay(window,false);
    }

    @Override
    public void onNotify(int what, int arg1, int arg2, Object obj) {
        handler.sendMessage(handler.obtainMessage(what,arg1,arg2,obj));
    }

    @Override
    public void onHandler(int what, int arg1, int arg2, Object obj) {
        switch (what){
            //连接结果,视频异常断开时关闭此界面
            case JVNetConst.CALL_CATEYE_CONNECTED: {
                if(arg2!= JVNetConst.CCONNECTTYPE_CONNOK){
                    this.finish();
                }
                break;
            }
            case 111:{
                SovUtil.startStreamRemotePlay(window,playPath);
                break;
            }
            //远程回放结束回调
            case JVNetConst.CALL_CATEYE_PLAY_END:{
                isConnected = true;
                Toast.makeText(JVCatFilePlayActivity.this, "视频播放完成", Toast.LENGTH_SHORT).show();
                this.finish();
                break;
            }
            //猫眼流媒体远程回放数据回调
            case JVNetConst.CALL_CATEYE_PLAY: {
//                MyLog.v(JVLogConst.LOG_STREAM_CAT, getLocalClassName() + "--callback:CALL_CATEYE_PLAY:what:" + what + ";arg1:" + arg1 + ";arg2:" + arg2 + ";obj:" + obj.toString());
//                what:218;arg1:0;arg2:3;obj:{"audio_bit":16,"audio_channel":1,"audio_sample_rate":8000,"audio_type":2,"count":2399,"fps":20.0,"height":540,"total":2399,"width":960}
                switch (arg2) {
                    case JVNetConst.JVN_DATA_O: {
                        try {
                            org.json.JSONObject jObj = new org.json.JSONObject(obj.toString());
                            if (null != jObj) {
                                totalProgress = jObj.optInt("total");
                            }

                        } catch (org.json.JSONException e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                    case JVNetConst.JVN_DATA_I:
                    case JVNetConst.JVN_DATA_B:
                    case JVNetConst.JVN_DATA_P: {
                        currentProgress++;
                        Log.e(TAG,"totalProgress="+totalProgress+";currentProgress="+currentProgress);
                        break;
                    }

                }
                break;
            }
            //出图
            case JVNetConst.CALL_NEW_PICTURE: {
                isConnected = true;
                break;
            }
            default:{
                break;
            }
        }
    }
}
